package com.redoor.app.stake.basicdata.controller;

import com.redoor.app.stake.basicdata.commons.BasicDataStatus;
import com.redoor.app.stake.basicdata.model.serviceModel.BaseChargerServiceBean;
import com.redoor.app.stake.common.controller.BaseController;
import com.redoor.app.stake.common.model.Page;
import com.redoornetwork.framework.core.response.ResponseResult;
import com.redoornetwork.framework.core.response.ResultGenerator;
import com.redoor.app.stake.basicdata.model.BaseCharger;
import com.redoor.app.stake.basicdata.model.BaseChargerCriteria;
import com.redoor.app.stake.basicdata.service.BaseChargerService;
import com.redoornetwork.framework.core.response.Status;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by wangjunlei on 2018-02-12 10:08:10.
 */
@RestController
@RequestMapping("/base/chargers")
public class BaseChargerController extends BaseController {

    @Autowired
    private BaseChargerService baseChargerService;

    @ApiOperation(value = "创建充电枪数据",notes = "创建充电枪数据")
    @RequiresPermissions("base:charggun:insert")
    @PostMapping(value = "/add")
    public ResponseResult add(@RequestBody BaseCharger baseCharger) {
        baseChargerService.insertCharger(baseCharger);
        return ResultGenerator.generatorSuccess();
    }

    @RequiresPermissions("base:charggun:delete")
    @PostMapping(value = "/delete")
    public ResponseResult delete(String uuid) {
        baseChargerService.deleteChargerByUuid(uuid);
        return ResultGenerator.generatorSuccess();
    }

    @ApiOperation(value = "根据uuid修改充电枪数据",notes = "根据uuid修改充电枪数据")
    @RequiresPermissions("base:charggun:update")
    @PostMapping(value = "/update")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uuid",value = "主键",paramType = "query",dataType = "String",required = true,defaultValue = "")
    })
    public ResponseResult update(@RequestBody BaseCharger baseCharger) {
        int res = baseChargerService.updateCharger(baseCharger);
        return ResultGenerator.generatorSuccess();
    }

    @ApiOperation(value = "根据枪uuid查询枪实体",notes = "根据枪uuid查询枪实体")
    @PostMapping("/get")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uuid",value = "主键",paramType = "query",dataType = "String",required = true,defaultValue = "")
    })
    public ResponseResult detail(@RequestParam(value = "uuid",defaultValue = "") String uuid) {
        BaseCharger baseCharger = baseChargerService.getChargerByUuid(uuid);
        return ResultGenerator.generatorSuccess(baseCharger);
    }


    @ApiOperation(value = "根据条件分页查询充电枪列表",notes = "根据条件分页查询充电枪列表")
    @RequiresPermissions("base:charggun:list")
    @PostMapping(value = "/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "limit",value = "偏移量",paramType = "query",dataType = "int",required = true,defaultValue = "10"),
            @ApiImplicitParam(name = "offset",value = "起始位置",paramType = "query",dataType = "int",required = true,defaultValue = "1"),
            @ApiImplicitParam(name = "chargerNo",value = "充电枪编号",paramType = "query",dataType = "String",required = false,defaultValue = ""),
            @ApiImplicitParam(name = "assetNo",value = "资产编号",paramType = "query",dataType = "String",required = false,defaultValue = ""),
            @ApiImplicitParam(name = "type",value = "设备接口类型",paramType = "query",dataType = "String",required = false,defaultValue = "")
    })
    public ResponseResult list(BaseChargerCriteria criteria) {
        List<BaseChargerServiceBean> list = baseChargerService.queryChargers(criteria);
        int total = baseChargerService.queryChargersTotal(criteria);

        Page page = new Page();
        page.setData(list);
        page.setTotal(total);
        return ResultGenerator.generatorSuccess(page);
    }

    @ApiOperation(value = "根据桩主键查找枪数据",notes = "根据桩主键查找枪数据")
    @PostMapping(value = "/queryChargersByStakeUuid")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stakeUuid",value = "充电桩主键",paramType = "query",dataType = "String",required = true,defaultValue = "")
    })
    public ResponseResult queryChargersByStakeUuid(@RequestParam(value = "stakeUuid",defaultValue = "") String stakeUuid) {

        if (stakeUuid==null || stakeUuid.trim().length()==0){

            return ResultGenerator.generatorFail(BasicDataStatus.PARAM_IS_EMPTY);
        }

        List<BaseChargerServiceBean> list = baseChargerService.queryChargersByStakeUuid(stakeUuid);

        return ResultGenerator.generatorSuccess(list);
    }

}
